import os
import pandas as pd


class DataOperation:
    def __init__(self, path):
        # 判断后缀
        if path.endswith('.csv') or ('.txt') or ('.tsv'):
            self.data = pd.read_csv(path)
        elif path.endswith('excel'):
            self.data = pd.read_excel(path)
        else:
            print('不支持该文件类型')

    def __init__(self, path):
        # 以扩展名进行判断
        extention = os.path.split(path)[-1]
        if extention in ('.csv') or ('.txt') or ('.tsv'):
            self.data = pd.read_csv(path)
        elif extention in ('.excel'):
            self.data = pd.read_excel(path)
        else:
            print('不支持该文件类型')

    def get_data_to_dict(self):
        # 获取数据以字典返回
        return [self.data.iloc[i].to_dict() for i in self.data.index.values]

    def get_data_to_list(self):
        # 获取数据以列表返回
        return self.data.values.tolist()


if __name__ == '__main__':
    project_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    path = os.path.join(project_path, 'data/lj_data.csv')

    data = DataOperation(path)
    print(data.get_data_to_dict())

    print(data.get_data_to_list())
